﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using EAT.Common;
using System.Collections.Specialized;
using System.DirectoryServices;
using System.Configuration;

namespace ECTSUserMgmtExtension
{
    internal class UserDataTable : DataTable
    {
        internal enum UserDataTableColumns
        {
            UserId,
            FirstName,
            LastName,
            Service,
            Phone,
            Requester,
            Enabled,
            Notes,
            Site,
            FinalEnum
        };

        internal UserDataTable(cUserManager userManager, string filter)
            : base()
        {
            for (UserDataTableColumns columns = UserDataTableColumns.UserId; columns < UserDataTableColumns.FinalEnum; ++columns)
            {
                DataColumn column = new DataColumn(columns.ToString());
                Columns.Add(column);
            }

            string trueFilter;
            try
            {
                string requestSite = ConfigurationManager.AppSettings["RequestSite"].ToString();
                string adField = ConfigurationManager.AppSettings["RequestSiteAdField"].ToString();
                bool adFieldMultiValue = bool.Parse(ConfigurationManager.AppSettings["ADFieldMultiValue"].ToString());

                trueFilter = string.Format("(&({0}={1}){2})", adField, requestSite, filter);
            }
            catch
            {
                trueFilter = filter;
            }

            DirectoryEntry rootEntry = userManager.Connection.Directory;
            DirectorySearcher ds = new DirectorySearcher(rootEntry, trueFilter);
            SearchResultCollection results = ds.FindAll();
            foreach (SearchResult result in results)
            {
                using (DirectoryEntry user = result.GetDirectoryEntry())
                {
                    if (user != null && user.Path != rootEntry.Path)
                    {
                        string cn = string.Empty;
                        string givenName = string.Empty;
                        string sn = string.Empty;
                        string company = string.Empty;
                        string telephoneNumber = string.Empty;
                        string eatmuRequester = string.Empty;
                        string enabled = string.Empty;
                        string notes = string.Empty;

                        try { cn = user.Properties["cn"].Value.ToString(); }
                        catch { }
                        try { givenName = user.Properties["givenName"].Value.ToString(); }
                        catch { }
                        try { sn = user.Properties["SN"].Value.ToString(); }
                        catch { }
                        try { company = user.Properties["company"].Value.ToString(); }
                        catch { }
                        try { telephoneNumber = user.Properties["telephoneNumber"].Value.ToString(); }
                        catch { }
                        try { eatmuRequester = user.Properties["eatmuRequester"].Value.ToString(); }
                        catch { }
                        try { enabled = ((bool)user.Properties["msDS-UserAccountDisabled"].Value == true) ? string.Empty : "X"; }
                        catch { }
                        try { notes = user.Properties["comment"].Value.ToString(); }
                        catch { }

                        Rows.Add(cn, givenName, sn, company, telephoneNumber, eatmuRequester, enabled, notes);
                    }
                }
            }
        }
    }
}
